using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Dao;
using Entities;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Data.SqlClient;
namespace Doctors_WebForum.Admin.UIControls
{
    public partial class Roles : System.Web.UI.UserControl
    {
        private IList<Role> listRole;
        private RoleData roleData;
        private Role role;
        public Roles()
        {
            listRole = new List<Role>();
            roleData = new RoleData();
            role = new Role();
        }
        private void LoadData()
        {
            dtlUser.DataSource = roleData.GetAll();
            dtlUser.DataBind();
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }
        # region Implement function sort and find
        //Sort by RoleID
        protected void RoleID(object sender, EventArgs e)
        {
            listRole = roleData.SortByColumn("RoleID");
            dtlUser.DataSource = listRole;
            dtlUser.DataBind();
            HiddenAfterSort();
        }
        //Sort by RoleName
        protected void RoleName(object sender, EventArgs e)
        {
            listRole = roleData.SortByColumn("RoleName");
            dtlUser.DataSource = listRole;
            dtlUser.DataBind();
            HiddenAfterSort();
        }
        protected void txtSearch_TextChanged(object sender, EventArgs e)
        {
            string searchUser = txtSearch.Text.Trim();

            if (searchUser != string.Empty)
            {
                listRole = roleData.SearchByTitle(searchUser);
                if (listRole.Count != 0)
                {
                    int countUser = listRole.Count;
                    dtlUser.Visible = true;
                    dtlUser.DataSource = listRole;
                    dtlUser.DataBind();
                    lblMessage.Visible = true;
                    lblMessage.Text = "<b> Have total <font style = 'color:#C87431;'>" + countUser + "</font> tilte for result " + "<font style = 'color:#C87431;'>" + searchUser + "</font><b/>";
                    txtSearch.Focus();
                }
                else
                {
                    lblMessage.Visible = true;
                    dtlUser.Visible = false;
                    lblMessage.Text = "<b>No result for <font style='color: #C87431;'>" + searchUser + "</font></b>"; txtSearch.Focus();
                }
            }
            else
            {
                lblMessage.Visible = true;
                dtlUser.Visible = false;
                lblMessage.Text = "<b>Please enter name to search !</b>";
                txtSearch.Focus();
            }
        }
        #endregion

        #region Imlement fuction edit data
        protected void dtlUser_EditCommand(object source, DataListCommandEventArgs e)
        {
            dtlUser.EditItemIndex = e.Item.ItemIndex;
            lblMessage.Visible = false;
            LoadData();
        }

        protected void dtlUser_CancelCommand(object source, DataListCommandEventArgs e)
        {
            lblMessage.Visible = false;
            dtlUser.EditItemIndex = -1;
            LoadData();
        }

        protected void dtlUser_UpdateCommand(object source, DataListCommandEventArgs e)
        {
            // get the primary key column value, and editing data
            string id = dtlUser.DataKeys[e.Item.ItemIndex].ToString();
            // get the data which user has been edit
            string roleName = ((TextBox)e.Item.FindControl("txtRoleName")).Text;
            string decript = ((TextBox)e.Item.FindControl("txtDescription")).Text;
            lblMessage.Visible = false;
            // write code to update data here
            role = new Role();
            role.RoleID = Utility.ToByte(id);
            role.RoleName = roleName;
            role.Description = decript;
            if (roleData.Update(role) == true)
            {
                lblMessage.Visible = true;
                lblMessage.Text = "<font style='color:red;'> update success !</font>";
                dtlUser.EditItemIndex = -1;
                LoadData();
            }
            else
            {
                lblMessage.Visible = true;
                lblMessage.Text = "<font style='color:red;'>update not success !</font>";
            }

        }

        protected void dtlUser_DeleteCommand(object source, DataListCommandEventArgs e)
        {
            // get the primary key column value, and editing data
            string id = dtlUser.DataKeys[e.Item.ItemIndex].ToString();
            // write code to delete data here
            if (roleData.DeleteByID(Utility.ToByte(id)) == true)
            {
                lblMessage.Visible = true;
                lblMessage.Text = "<font style='color:red;'> delete success !</font>";
                dtlUser.EditItemIndex = -1;
                LoadData();
            }
            else if (RoleData.paraResult.Equals("-1"))
            {
                lblMessage.Text = "<font style='color:red;'>delete can not success , because have using for members , you must reset in members and then trying a gain. !</font>";
            }
            else
            {
                if (!RoleData.paraResult.Equals("-1"))
                    lblMessage.Text = "<font style='color:red;'>delete not success !</font>";
            }
        }
        #endregion

        protected void lbkListUser_Click(object sender, EventArgs e)
        {
            HiddenAfterSort();
            LoadData();
        }
        private void HiddenAfterSort()
        {
            dtlUser.Visible = true;
            lblMessage.Visible = false;
        }

        protected void btnAddRole_Click(object sender, EventArgs e)
        {
            role = new Role();
            role.RoleName = txtRoleName.Text;
            role.Description = txtDescription.Text;
            roleData = new RoleData();
            if (!roleData.Insert(role))
            {
                lblError.Text = "Add role success !";
            }
            else
            {
                lblError.Text = "Add role can not success !";
            }
            LoadData();
        }



    }
}